package cn.sms.basic.controller;

import cn.sms.basic.dto.LoginDto;
import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.service.ILoginService;
import cn.sms.basic.util.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
@Api(tags = "登录接口",description = "登录接口描述")
public class LoginController {
    @Autowired
    private ILoginService iLoginService;

    @PostMapping("/account")
    public AjaxResult goToLogin(@RequestBody LoginDto dto){
        try {
            Map map = iLoginService.goToLogin(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    @PostMapping("/wechat")
    public AjaxResult goWechat(@RequestBody Map<String,String> param){
        try {

            return iLoginService.goWechat(param);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }


    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> param){
        try {
            Map map = iLoginService.binder(param);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }
}
